package com.xyht.sca_s.student_manage_system.modules.enrollmentManagement.controller;


import com.xyht.sca_s.student_manage_system.auth.util.JwtTokenUtil;
import com.xyht.sca_s.student_manage_system.common.annotation.RequestPass;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.desEncodeAndDecode.annotation.SecretBody;
import com.xyht.sca_s.student_manage_system.modules.enrollmentManagement.entity.req.SmsEnrollmentManagementOrgSettingBatchReq;
import com.xyht.sca_s.student_manage_system.modules.enrollmentManagement.entity.req.SmsEnrollmentManagementOrgSettingReq;
import com.xyht.sca_s.student_manage_system.modules.enrollmentManagement.service.SmsEnrollmentManagementOrgSettingService;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.Future;

/**
 * <p>
 * 招生院系专业组织架构设置 前端控制器
 * </p>
 *
 * @author XYHT
 * @since 2024-01-24
 */
@RestController
@RequestMapping("/enrollment/orgSetting")
@SecretBody
public class SmsEnrollmentManagementOrgSettingController {
    @Resource
    private SmsEnrollmentManagementOrgSettingService smsEnrollmentManagementOrgSettingService;
    @Resource
    private JwtTokenUtil jwtTokenUtil;

    @PostMapping("/add")
    @ApiModelProperty(value = "添加招生管理院系专业")
    public ResponseResult addEnrollmentManagementCollegeMajor(@RequestBody SmsEnrollmentManagementOrgSettingReq settingReq) {
        return smsEnrollmentManagementOrgSettingService.addEnrollmentOrgSetting(settingReq);
    }

    @PostMapping("/delete")
    @ApiModelProperty(value = "删除招生管理院系专业")
    public ResponseResult deleteEnrollmentOrgSetting(@RequestBody SmsEnrollmentManagementOrgSettingReq settingReq) {
        return smsEnrollmentManagementOrgSettingService.deleteEnrollmentOrgSetting(settingReq);
    }

    @PostMapping("/update")
    @ApiModelProperty(value = "修改招生管理院系专业")
    public ResponseResult updateEnrollmentOrgSetting(@RequestBody SmsEnrollmentManagementOrgSettingReq settingReq) {
        return smsEnrollmentManagementOrgSettingService.updateEnrollmentOrgSetting(settingReq);
    }

    @GetMapping("/list")
    @ApiModelProperty(value = "获取招生管理院系专业列表")
    public ResponseResult addEnrollmentManagementCollegeMajor(@RequestParam(value = "orgType") Integer orgType,
                                                              @RequestParam(value = "parentId", required = false) String parentId,
                                                              @RequestParam(value = "searchStr", required = false) String searchStr,
                                                              @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                              @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        return smsEnrollmentManagementOrgSettingService.getEnrollmentOrgSettingList(orgType, parentId, searchStr, pageNum, pageSize);
    }

    @GetMapping("/studentDisplay")
    @ApiModelProperty(value = "学生预报名获取院系专业下拉框数据")
    @RequestPass
    public ResponseResult getEnrollmentOrgCollegeAndMajorStudentDisplay(@RequestParam(value = "orgType") Integer orgType,
                                                                        @RequestParam(value = "parentId", required = false) String parentId) {
        return smsEnrollmentManagementOrgSettingService.getEnrollmentOrgCollegeAndMajorStudentDisplay(orgType, parentId);
    }

    @GetMapping("/backDisplay")
    @ApiModelProperty(value = "后台招生管理获取院系专业下拉框数据")
    @RequestPass
    public ResponseResult getEnrollmentOrgCollegeAndMajorBackDisplay(@RequestParam(value = "orgType") Integer orgType,
                                                                     @RequestParam(value = "parentId", required = false) String parentId) {
        return smsEnrollmentManagementOrgSettingService.getEnrollmentOrgCollegeAndMajorBackDisplay(orgType, parentId);
    }

    @PostMapping("/batch")
    @ApiModelProperty(value = "院系专业设置批量导入")
    public ResponseResult batchEnrollmentManagementCollegeMajorSetting(@RequestBody SmsEnrollmentManagementOrgSettingBatchReq orgSettingBatchReq ,HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        ResponseResult commonCodeEnum;
        try{
            Future<ResponseResult> future = smsEnrollmentManagementOrgSettingService.batchEnrollmentManagementCollegeMajorSetting(orgSettingBatchReq,user_id);
            while (true){
                if (future.isDone()){
                    commonCodeEnum = future.get();
                    break;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return commonCodeEnum;
    }
}

